<%-- web/admin/user-edit.jsp --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.bookmanager.model.User" %>
<%
    // 检查管理员权限
    User currentUser = (User) session.getAttribute("user");
    if (currentUser == null || !"admin".equals(currentUser.getRole())) {
        response.sendRedirect("../login.jsp");
        return;
    }

    // 获取要编辑的用户信息
    User editUser = (User) request.getAttribute("editUser");
    if (editUser == null) {
        response.sendRedirect("user-management");
        return;
    }
%>
<html>
<head>
    <title>编辑用户 - 图书管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f7fa;
            color: #333;
        }

        .header {
            background: white;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 0 20px;
        }

        .nav-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
            height: 70px;
        }

        .logo {
            font-size: 1.5rem;
            font-weight: bold;
            color: #667eea;
        }

        .admin-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .admin-badge {
            background: #28a745;
            color: white;
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 0.8rem;
            font-weight: 500;
        }

        .logout-btn {
            padding: 8px 16px;
            background: #dc3545;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            font-size: 0.9rem;
        }

        .container {
            max-width: 600px;
            margin: 30px auto;
            padding: 0 20px;
        }

        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid #eee;
        }

        .page-title {
            font-size: 1.8rem;
            color: #333;
        }

        .form-card {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #555;
        }

        .form-control {
            width: 100%;
            padding: 12px;
            border: 2px solid #e1e5e9;
            border-radius: 6px;
            font-size: 1rem;
            transition: border-color 0.3s ease;
        }

        .form-control:focus {
            outline: none;
            border-color: #667eea;
        }

        .form-control[readonly] {
            background-color: #f8f9fa;
            color: #6c757d;
        }

        .form-hint {
            font-size: 0.85rem;
            color: #666;
            margin-top: 5px;
        }

        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 1rem;
            text-decoration: none;
            display: inline-block;
            text-align: center;
            transition: background-color 0.3s ease;
        }

        .btn-primary {
            background: #667eea;
            color: white;
        }

        .btn-primary:hover {
            background: #5a6fd8;
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        .btn-secondary:hover {
            background: #5a6268;
        }

        .form-actions {
            display: flex;
            gap: 15px;
            justify-content: flex-end;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #eee;
        }

        .alert {
            padding: 12px 16px;
            border-radius: 6px;
            margin-bottom: 20px;
        }

        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .user-info {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 6px;
            margin-bottom: 20px;
            border-left: 4px solid #667eea;
        }

        .info-item {
            margin-bottom: 5px;
        }

        .info-label {
            font-weight: 500;
            color: #555;
        }
    </style>
</head>
<body>
<!-- 顶部导航 -->
<div class="header">
    <div class="nav-bar">
        <div class="logo">📚 图书管理系统</div>
        <div class="admin-info">
            <span>欢迎，<strong><%= currentUser.getUsername() %></strong></span>
            <span class="admin-badge">管理员</span>
            <a href="<%= request.getContextPath() %>/logout" class="logout-btn">退出登录</a>
        </div>
    </div>
</div>

<div class="container">
    <!-- 页面头部 -->
    <div class="page-header">
        <h1 class="page-title">✏️ 编辑用户</h1>
        <a href="user-management" class="btn btn-secondary">返回用户列表</a>
    </div>

    <!-- 显示错误或成功消息 -->
    <%
        String message = (String) request.getAttribute("message");
        String messageType = (String) request.getAttribute("messageType");

        if (message != null) {
            String alertClass = "error".equals(messageType) ? "alert-error" : "alert-success";
    %>
    <div class="alert <%= alertClass %>">
        <%= message %>
    </div>
    <% } %>

    <!-- 用户基本信息 -->
    <div class="user-info">
        <div class="info-item">
            <span class="info-label">用户ID:</span> <%= editUser.getUserId() %>
        </div>
        <div class="info-item">
            <span class="info-label">注册时间:</span> <%= editUser.getCreateTime() != null ? editUser.getCreateTime().toString() : "未知" %>
        </div>
    </div>

    <!-- 编辑用户表单 -->
    <div class="form-card">
        <form action="<%= request.getContextPath() %>/admin/update-user" method="post" id="editUserForm">
            <input type="hidden" name="userId" value="<%= editUser.getUserId() %>">

            <div class="form-group">
                <label class="form-label" for="userId">用户ID</label>
                <input type="text" id="userId" class="form-control"
                       value="<%= editUser.getUserId() %>" readonly>
                <div class="form-hint">用户ID不可修改</div>
            </div>

            <div class="form-group">
                <label class="form-label" for="username">用户名 *</label>
                <input type="text" id="username" name="username" class="form-control"
                       value="<%= editUser.getUsername() %>" required maxlength="50">
            </div>

            <div class="form-group">
                <label class="form-label" for="email">邮箱 *</label>
                <input type="email" id="email" name="email" class="form-control"
                       value="<%= editUser.getEmail() != null ? editUser.getEmail() : "" %>" required>
            </div>

            <div class="form-group">
                <label class="form-label" for="phone">手机号</label>
                <input type="tel" id="phone" name="phone" class="form-control"
                       value="<%= editUser.getPhone() != null ? editUser.getPhone() : "" %>"
                       placeholder="请输入手机号（可选）" maxlength="11">
            </div>

            <div class="form-group">
                <label class="form-label" for="role">用户角色 *</label>
                <select id="role" name="role" class="form-control" required>
                    <option value="reader" <%= "reader".equals(editUser.getRole()) ? "selected" : "" %>>普通用户</option>
                    <option value="admin" <%= "admin".equals(editUser.getRole()) ? "selected" : "" %>>管理员</option>
                </select>
            </div>

            <div class="form-group">
                <label class="form-label" for="password">新密码</label>
                <input type="password" id="password" name="password" class="form-control"
                       placeholder="留空表示不修改密码" minlength="6">
                <div class="form-hint">如需修改密码，请输入新密码（至少6位）</div>
            </div>

            <div class="form-actions">
                <button type="reset" class="btn btn-secondary">重置</button>
                <button type="submit" class="btn btn-primary">保存修改</button>
            </div>
        </form>
    </div>
</div>

<script>
    // 表单验证
    document.getElementById('editUserForm').addEventListener('submit', function(e) {
        const password = document.getElementById('password').value;

        if (password && password.length < 6) {
            e.preventDefault();
            alert('密码长度至少6位！');
            return false;
        }
    });
</script>
</body>
</html>
